package com.group.controller;

import com.group.service.GoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.entity.Good;
import pojo.entity.GoodInfoExpand;
import utils.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "商品信息")
@Controller
@RequestMapping(value = "/good", produces = "text/html;charset=UTF-8")
public class GoodController extends BaseController {

    @Autowired
    private GoodService goodService;

    @ApiOperation("保存商品")
    @RequestMapping
    @ResponseBody
    public String addGoods(String title, String page) {
        goodService.addGood(title, page);
        return "商品保存成功";
    }

    @ApiOperation("查询全部商品")
    @RequestMapping("/findAllGoods.do")
    @ResponseBody
    public String findAllGoods(HttpServletResponse response) {
        List<Good> allGoods = goodService.findAllGoods();
        int size = allGoods.size();
        List<Good> someGood=new ArrayList<>();
        int max=size,min=0;
        for (int i = 0; i < 35; i++) {
            int zzz=(int) (Math.random()*(max-min)+min);
            someGood.add(allGoods.get(zzz));
        }
        String allGoodsJson = writerSuccessResult(someGood, response);
        return allGoodsJson;
    }

    @ApiOperation("搜索商品")
    @RequestMapping("/searchGoods.do")
    @ResponseBody
    public String searchGoods(HttpServletRequest request, HttpServletResponse response) {
        String goodName = convertParamsToEntity(request, String.class);
        List<Good> goodsList = goodService.searchGoods(goodName);
        String goodsListJson = writerSuccessResult(goodsList, response);
        return goodsListJson;
    }

    @ApiOperation("根据商品id查询商品信息")
    @GetMapping("/findGoodInfoById.do")
    @ResponseBody
    public String findGoodInfoById(int goodId, HttpServletResponse response) {
        System.out.println("goodId============================================" + goodId);
        Good g = goodService.findGoodInfoById((long) goodId);
        String goodJson = writerSuccessResult(g, response);
        return goodJson;
    }

    @ApiOperation("查询商品详细信息，返回商品信息集合")
    @GetMapping("/findGoodInfoExpand.do")
    @ResponseBody
    public String findGoodInfoExpand(int goodId, HttpServletResponse response) {
        List<GoodInfoExpand> InfoExpandList = goodService.findGoodInfoExpand((long) goodId);
        return writerSuccessResult(InfoExpandList, response);
    }

    @ApiOperation("查询商品评分")
    @GetMapping("/findGoodStarScore.do")
    @ResponseBody
    public String findGoodStarScore(int goodId, HttpServletResponse response) {
        Integer starScore = goodService.findGoodsStarScore((long) goodId);
        return writerSuccessResult(starScore, response);
    }

    @ApiOperation("日成交量")
    @GetMapping("/findGoodDeal.do")
    @ResponseBody
    public String findGoodDeal(int goodId, HttpServletResponse response) {
        Integer goodDeal = goodService.findGoodDayDeal((long) goodId);
        return writerSuccessResult(goodDeal, response);
    }
}
